parse-css-color
Parse a CSS color string.
Supports
- <color value>
- Hexadecimal RGB value: #RGB #RRGGBB
- #RGBA #RRGGBBAA (4 and 8-digit hexadecimal RGBA notation)
- RGB/A color module level 3 and 4 (number, percentage)
- HSL/A color module level 3 and 4 (number, deg, rad, turn)
- <color keyword>
- transparent
- Shorthand for transparent black, rgba(0,0,0,0).
Does not support
Installation
NPM
npm i parse-css-color
Or as a <script>
tag from a CDN as parseCssColor
:
Unpkg CDN
<script src="https://unpkg.com/parse-css-color"></script>
jsDelivr CDN
<script src="https://cdn.jsdelivr.net/npm/parse-css-color"></script>
Usage
import parse from 'parse-css-color'
parse('#00f')
parse('#00f8')
parse('#0000FF80')
parse('#00g')
parse('hsl(270deg 60% 70% / 25%)')
parse('hsl(4.71239rad 260% -70% / 0.5)')
parse('hsla(.75turn, 60%, 70%, 50%)')
parse('hsla(100deg 0 0 / 0)')
parse('rgb(255 0 0 / 0.5)')
parse('rgb(500 -100 0 / 200%)')
parse('rgba(255, 0, 255, 20%)')
parse('rgba(100% 255 100% / 0)')
See tests for more cases.
Dev
npm install
npm test
npm run dev
Related
- mix-css-color - Mix two CSS colors together in variable proportion. Opacity is included in the calculations.
- values.js - Get the tints and shades of a CSS color.
License
MIT © Noel Delgado